//
//  XKRechargeView.m
//  xk100
//
//  Created by xiaokang on 2017/5/27.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#define kMaxAmount @"100000"

#import "XKRechargeView.h"
#import "XKRechargeTypeSelectView.h"

@interface XKRechargeView () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel       *moneyTitleLabel;           // 充值金额提示
@property (nonatomic, strong) UIView        *moneyBgView;               // 白背景
@property (nonatomic, strong) UILabel       *moneyFlagLabel;            // 充值金钱标志
@property (nonatomic, strong) UITextField   *moneyTextField;            // 充值金额输入框
@property (nonatomic, strong) UILabel       *rechargeSelectedTitle;     // 充值方式选择提示
@property (nonatomic, strong) XKRechargeTypeSelectView *wexinpayView;   // 微信支付
@property (nonatomic, strong) XKRechargeTypeSelectView *alipayView;     // 支付宝支付
@property (nonatomic, strong) XKRechargeTypeSelectView *cardpayView;    // 银行卡支付
@property (nonatomic, strong) UIButton *rechargeConfirmButton;

@end
@implementation XKRechargeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kBaseGrayColor;
        [self initSubviews];
    }
    return self;
}

#pragma mark -
- (void)initSubviews {
    _moneyTitleLabel = [UILabel getLabWithText:@"金额:(单笔限额100-100,000元)"
                                      textFont:Font_14
                                     textColor:[UIColor grayColor]
                                 textAlignment:NSTextAlignmentLeft];
    [self addSubview:_moneyTitleLabel];
    
    _moneyBgView = [UIView new];
    _moneyBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_moneyBgView];
    
    _moneyFlagLabel = [UILabel getLabWithText:@"¥:"
                                     textFont:Font_26
                                    textColor:[UIColor darkGrayColor]
                                textAlignment:NSTextAlignmentLeft];
    [_moneyBgView addSubview:_moneyFlagLabel];
    
    _moneyTextField = [[UITextField alloc] init];
    _moneyTextField.keyboardType    = UIKeyboardTypeDecimalPad;
    _moneyTextField.borderStyle     = UITextBorderStyleNone;
    _moneyTextField.textColor       = [UIColor blackColor];
    _moneyTextField.delegate        = self;
    _moneyTextField.font            = Font(32);
    [_moneyTextField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [_moneyBgView addSubview:_moneyTextField];
    
    _rechargeSelectedTitle = [UILabel getLabWithText:@"支付方式:"
                                            textFont:Font_14
                                           textColor:[UIColor grayColor]
                                       textAlignment:NSTextAlignmentLeft];
    [self addSubview:_rechargeSelectedTitle];
    
    _wexinpayView = [XKRechargeTypeSelectView new];
    _wexinpayView.iconImageName = @"wechatpay";
    _wexinpayView.titleString = @"微信支付";
    _wexinpayView.detailString = @"推荐微信钱包用户使用";
    _wexinpayView.buttonTag = 1;
    [self addSubview:_wexinpayView];
    
    _alipayView = [XKRechargeTypeSelectView new];
    _alipayView.iconImageName = @"alipay";
    _alipayView.titleString = @"支付宝支付";
    _alipayView.detailString = @"推荐支付宝用户使用";
    _alipayView.buttonTag = 2;
    [self addSubview:_alipayView];
    
    _cardpayView = [XKRechargeTypeSelectView new];
    _cardpayView.iconImageName = @"bank_card_pay";
    _cardpayView.titleString = @"银行卡支付";
    _cardpayView.detailString = @"支持储蓄卡和信用卡,无需开通网银";
    _cardpayView.buttonTag = 3;
    [self addSubview:_cardpayView];
    
    _rechargeConfirmButton = [UIButton getClickBtnWithTitle:@"充值"];
    _rechargeConfirmButton.enabled = YES;
    [_rechargeConfirmButton addTarget:self
                               action:@selector(rechargeConfirmButtonClickEvent:)
                     forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rechargeConfirmButton];
    
    @weakify(self);
    _wexinpayView.SelectedBlock = ^(NSInteger buttonTag) {
        @strongify(self);
        _channel = @"wei";
        self.alipayView.selected = NO;
        self.cardpayView.selected = NO;
    };
    
    _alipayView.SelectedBlock = ^(NSInteger buttonTag) {
        @strongify(self);
        _channel = @"pingaliypay";
        self.wexinpayView.selected = NO;
        self.cardpayView.selected = NO;
    };
    
    _cardpayView.SelectedBlock = ^(NSInteger buttonTag) {
        @strongify(self);
        _channel = @"gpay";
        self.wexinpayView.selected = NO;
        self.alipayView.selected = NO;
    };
    
    
    [_moneyTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self).offset(20*Scale_W);
    }];

    [_moneyBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(_moneyTitleLabel.mas_bottom).offset(4*Scale_W);
        make.height.mas_equalTo(68*Scale_W);
    }];

    [_moneyFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_moneyBgView).offset(20*Scale_W);
        make.width.mas_equalTo(30*Scale_W);
        make.centerY.mas_equalTo(_moneyBgView);
    }];
    
    [_moneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_moneyFlagLabel.mas_right);
        make.right.mas_equalTo(_moneyBgView).offset(-50*Scale_W);
        make.top.bottom.mas_equalTo(_moneyBgView);
    }];
    
    [_rechargeSelectedTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_moneyTitleLabel);
        make.top.mas_equalTo(_moneyBgView.mas_bottom).offset(20*Scale_W);
    }];
    
    [_wexinpayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.rechargeSelectedTitle.mas_bottom).offset(4*Scale_W);
        make.height.mas_equalTo(60*Scale_W);
    }];

    [_alipayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(_wexinpayView.mas_bottom).offset(1*Scale_W);
        make.height.mas_equalTo(60*Scale_W);
    }];
    
    [_cardpayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(_alipayView.mas_bottom).offset(1*Scale_W);
        make.height.mas_equalTo(60*Scale_W);
    }];
    
    [_rechargeConfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_cardpayView.mas_bottom).offset(20*Scale_W);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
        make.height.mas_equalTo(40*Scale_W);
    }];
}

- (void)rechargeConfirmButtonClickEvent:(UIButton *)sender {
    if (_rechargeConfirmBlock) {
        _rechargeConfirmBlock(sender, _channel, _moneyTextField.text);
    }
}

- (void)setMonetCount:(NSString *)monetCount {
    _monetCount = monetCount;
    _moneyTextField.text = monetCount;
    if ([monetCount isEqualToString:@""]) {
        _wexinpayView.selected = NO;
        _alipayView.selected = NO;
        _cardpayView.selected = NO;
    }
}

- (void)setChannel:(NSString *)channel {
    _channel = channel;
    if ([channel isEqualToString:@""]) {
        _wexinpayView.selected = NO;
        _alipayView.selected = NO;
        _cardpayView.selected = NO;
    }
}

- (void)setFirstResponder:(BOOL)firstResponder {
    _firstResponder = firstResponder;
    if (firstResponder) {
        [_moneyTextField becomeFirstResponder];
    } else {
        [_moneyTextField resignFirstResponder];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    
    BOOL isNumber = NO;
    if ([text isTradePrice]) {
        isNumber = YES;
    }
    
    if (text.doubleValue == 0 && text.length > 1 && ![text containsString:@"."]) {
        isNumber = NO;
    }
    
    if (text.doubleValue > kMaxAmount.doubleValue) {
        isNumber = NO;
    }
    
    if (text.doubleValue >=1 && [text hasPrefix:@"0"]) {
        textField.text = string;
        isNumber = NO;
    }
    return isNumber;
}

- (void)textFieldDidChanged:(UITextField *)textField {
    _monetCount = textField.text;
}
@end
